<template>
  <div class="home">
    <!-- 条件筛选查询操作 -->
    <el-form ref="form" :model="UserInfo" label-width="80px" size="mini">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input v-model="UserInfo.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址">
            <el-input v-model="UserInfo.useraddress"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item size="large">
        <el-button type="primary" @click="selectInfo()">查询</el-button>
        <el-button type="info" @click="resetInfo()">重置</el-button>
        <el-button type="success" @click="addUserInfo()">新增</el-button>
      </el-form-item>
    </el-form>
    <!--显示列表数据 -->
    <el-table
        ref="multipleTable"
        :data="userInfoList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          label="#"
          type="index"
          width="50">
      </el-table-column>
      <el-table-column
          prop="username"
          label="用户名"
          width="120">
      </el-table-column>
      <el-table-column
          prop="userpwd"
          label="密码"
          width="120">
      </el-table-column>
      <el-table-column
          prop="useraddress"
          label="地址"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="usertel"
          label="电话"
          width="180">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template #default="scope">
          <el-button @click="editUserinfo(scope.row)" type="primary" icon="el-icon-edit" circle size="small">编辑
          </el-button>
          <el-button @click="deleteUserinfo(scope.row.id)" type="danger" icon="el-icon-delete" circle size="small">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!-- 新增或编辑的弹出框 -->
    <el-dialog title="用户信息" v-model="dialogFormVisible">
      <el-form :model="EditUserInfoBean">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="EditUserInfoBean.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input type="password" v-model="EditUserInfoBean.userpwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="EditUserInfoBean.useraddress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="EditUserInfoBean.usertel" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateUser()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src


import request from "@/config/request";

export default {
  name: 'Home',
  data() {
    return {
      // 分页相关
      // 数据条数
      total: 0,
      // 当前页
      currentPage: 1,
      pageSize: 5,
      UserInfo: {
        username: "",
        userpwd: "",
        useraddress: "",
        usertel: ""
      },
      userInfoList: [],
      dialogFormVisible: false,
      // 新增或编辑的数据对象
      EditUserInfoBean: {
        // id判断是新增还是修改
        id: null,
        username: "",
        userpwd: "",
        useraddress: "",
        usertel: ""
      },
      formLabelWidth: '80px'
    }
  },
  components: {},
  methods: {
    // 分页相关
    handleSizeChange: function (currentSize) {
      this.pageSize = currentSize
      // 当页面条数发生变化的时候触发
      console.log("handleSizeChange::::" + currentSize)
      this.selectInfo(this.currentPage, currentSize)

    },
    handleCurrentChange: function (currentIndex) {
      // 当前页面（切换当前页面）发生变化时触发
      console.log("handleCurrentChange::::" + currentIndex)
      this.selectInfo(currentIndex, this.pageSize)

    }

    ,
    selectInfo: function (currentIndex, currentPageSize) {
      // 点击查询按钮
      console.log("发送请求")
      var that = this
      // 处理参数
      // var param=Qs.stringify(this.UserInfo)
      // console.log(param)
      // console.log(Qs.stringify(param))
      // 页面加载时，若没有传递参数，则使用默认分页参数
      if (!currentIndex) {
        currentIndex = this.currentPage
      }
      console.log("currentIndex" + currentIndex)
      if (!currentPageSize) {
        currentPageSize = this.pageSize
      }


      // var  param="&pageNum="+currentIndex+"&pageSize="+currentPageSize
      //   console.log(param)
      // 发送请求,
      // 1、查询操作可以使用get请求来进行操作？直接使用params传参即可
      //2、发送post请求时：可以进行参数处理,
      //       (1、引入qs对参数进行序列化处理 2、可以json方式传、但是需要在后台接收参数时进行简单处理)
      request.get('/api/userinfo/getallinfoes', {
            params: {
              // userInfo: this.UserInfo,
              pageNum: currentIndex,
              pageSize: currentPageSize,
              username: this.UserInfo.username,
              useraddress: this.UserInfo.useraddress
            }
          }
      )
          .then(function (response) {
            console.log(response);
            // // 将响应回来的数据赋值给表格中的数据对象
            that.userInfoList = response.list
            that.total = response.total
            that.currentPage = response.pageNum
            that.pageSize = response.pageSize
            that.UserInfo.username = ""
          })
          .catch(function (error) {
            console.log(error);
          });


    },
    resetInfo: function () {
      // 点击重置按钮的
    },
    addUserInfo: function () {
      // 点击新增按钮
      // 弹出dialog框
      this.dialogFormVisible = true
      this.EditUserInfoBean = {
        // id判断是新增还是修改
        id: null,
        username: "",
        userpwd: "",
        useraddress: "",
        usertel: ""
      }

    },
    handleSelectionChange: function () {

    },
    editUserinfo: function (rowData) {
      // 点击编辑操作的
      // 弹出dialog框
      this.dialogFormVisible = true
      console.log(rowData)
      // 编辑操作的模态框中的数据进行赋值操作
      // this.EditUserInfoBean=rowData
      this.EditUserInfoBean = Object.assign({}, rowData)

    },
    // 新增或编辑的保存方法
    addOrUpdateUser: function () {
      var that = this
      // console.log(param)
      // console.log(Qs.stringify(param))
      // 新增或删除方法请求发送
      request.post('/api/userinfo/addorupdateuser', this.EditUserInfoBean)
          .then(function (response) {


            // console.log(response);
            // 响应数据判断，为ok；则关闭dialog框；刷新数据
            if (response == "ok") {
              that.dialogFormVisible = false
              that.selectInfo()
            }


          })
          .catch(function (error) {
            console.log(error);
          });
    },
    deleteUserinfo: function (deleteId) {
      console.log(deleteId)
      var that = this
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        request.post('/api/userinfo/deleteuser', {"id": deleteId})
            .then(function (response) {
              console.log(response);
              // 删除成功后刷新页面
              that.selectInfo()
            })
            .catch(function (error) {
              console.log(error);
            });
        // 删除成功提示
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        // 删除失败进行提示
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }

  }
  ,
  mounted: function () {
    this.selectInfo()
  }


  ,

}
</script>
